const _toString = Object.prototype.toString;

export function isPlainObject(obj) {
  return _toString.call(obj) === "[object Object]";
}

export function mergeProps(to, from) {
  for (const key in from) {
    const value = from[key];
    if (isPlainObject(value)) {
      to[key] = to[key] || {};
      mergeProps(to[key], from[key]);
    } else {
      to[key] = from[key];
    }
  }
  return to;
}

export function isDef(val) {
  return val !== undefined && val !== null;
}

export function debounce() {
  // 设置定时器标识
  let timer;
  // 难点返回事件绑定函数
  return function (func, delay = 300) {
    // 先清除定时器
    if (timer) {
      clearTimeout(timer);
      timer = null;
    }
    //设置定时器
    timer = setTimeout(() => {
      // 调用函数
      func();
    }, delay);
  };
}

export function throttle(fn, wait = 50) {
  let timer = null;
  return function () {
    let context = this;
    let args = arguments;
    if (!timer) {
      timer = setTimeout(function () {
        fn.apply(context, args);
        timer = null;
      }, wait);
    }
  };
}